#include "GLibInit.h"
#include "GCritSecWrapper.h"
#include "GSmartPointer.h"
#include "GEventWrapper.h"
#include "GThreadWrapper.h"

class CGThreadWrapperTest2 : public CGThreadWrapper
{
public:
  CGThreadWrapperTest2(const gchar *threadName, gulong its=1)
  :CGThreadWrapper(threadName)
  ,m_iterations(its)
  ,m_currentIteration(0)
  {
    m_syncEvent.Create(FALSE, FALSE);
  }

  gboolean Wait(gulong milis=WAIT_INFINITE)
  {
    if(m_running)
      m_syncEvent.Wait(milis);

	return TRUE;
  }

protected:
  virtual gboolean RunLoop()
  {
	g_print("RunLoop (%s) (%u)!\n", m_name, m_currentIteration);
    g_usleep(G_USEC_PER_SEC);

	gboolean loop = (++m_currentIteration<m_iterations);
	if(!loop)
      m_syncEvent.Set();
  
    return loop;
  }

protected:
  gulong m_iterations;
  gulong m_currentIteration;
  CGEventWrapper m_syncEvent;
};

typedef CGSmartPtr<CGThreadWrapperTest2> CGThreadWrapperSPtr;

static void TestSmartPointer1()
{
  CGThreadWrapperSPtr thread(new CGThreadWrapperTest2("TestSmartPointer1", 5));
  thread->CreateThread();
  thread->Wait();

  // thread->AddRef(); /* LEAK */
}

void AddTestSmartPointer()
{
  g_test_add_func("/smartpointer/smartpointer1", TestSmartPointer1);
}
